/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.GradientPaint;

/**
 *
 * @author Administrator
 */
public class RTextField extends javax.swing.JTextField {

    private boolean focus_Gained;
    private final int arcWH = 8;
    private boolean customizeEditable = true;
    private boolean customizeAlwaysEditable = false;
    
    String trimText;

    public RTextField() {
        super();
        setOpaque(false);
        setMargin(new java.awt.Insets(1, 5, 1, 5));
        setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));
        
        setSize(50, 24);
        setPreferredSize(new java.awt.Dimension(50, 24));


        addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                focus_Gained = true;
                repaint();
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                focus_Gained = false;
                repaint();
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
    
        trimText = getText().trim();
        if (trimText != null && ! trimText.equals(getText())) {
            setText(trimText);
        }
        
        Graphics2D g2d = (Graphics2D) g;
        int width = getWidth();
        int height = getHeight();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, width, height, arcWH, arcWH);
        g2d.clip(rect);

        if (!java.beans.Beans.isDesignTime()) {
            refreshEditable();

            if (isEditable()) {
                GradientPaint gp = new GradientPaint(0.0F, 0.0F, new Color(200, 200, 200), 0.0F, height / 2, new Color(255, 255, 255), false);
                g2d.setPaint(gp);
                g2d.fillRoundRect(4, 4, width - 8, height - 8, arcWH, arcWH);
            }
        }

        super.paintComponent(g);
    }

    @Override
    protected void paintBorder(java.awt.Graphics g) {
        
        RoundRectangle2D.Double rect;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();


        if (java.beans.Beans.isDesignTime()) {
            g2d.setColor(new Color(0, 0, 0));
            rect = new RoundRectangle2D.Double(3, 3, width - 7, height - 7, arcWH, arcWH);
            g2d.draw(rect);
        } else {
            refreshEditable();

            if (!isEditable()) {
                g2d.setColor(new Color(199, 199, 199));
                rect = new RoundRectangle2D.Double(3, 3, width - 7, height - 7, arcWH, arcWH);
                g2d.draw(rect);
            } else {

                g2d.setColor(new Color(0, 0, 0));
                rect = new RoundRectangle2D.Double(3, 3, width - 7, height - 7, arcWH, arcWH);
                g2d.draw(rect);

                if (focus_Gained) {
                    g2d.setColor(new Color(136, 178, 96));
                    rect = new RoundRectangle2D.Double(2, 2, width - 5, height - 5, arcWH, arcWH);
                    g2d.draw(rect);

                    g2d.setColor(new Color(196, 229, 154));
                    rect = new RoundRectangle2D.Double(1, 1, width - 3, height - 3, arcWH, arcWH);
                    g2d.draw(rect);

                    g2d.setColor(new Color(196, 229, 154));
                    rect = new RoundRectangle2D.Double(0, 0, width - 1, height - 1, arcWH, arcWH);
                    g2d.draw(rect);
                }
            }
        }
    }

    private void refreshEditable() {
        if (customizeAlwaysEditable) {
            setEditable(true);
            return;
        }

        if (getRootPane().getParent() instanceof MaintDialog) {
            if (((MaintDialog) (getRootPane().getParent())).dlgDataMode == AppSetting.DISPLAY_MODE) {
                setEditable(false);
            } else {
                setEditable(customizeEditable);
            }
        } else if (getRootPane().getParent() instanceof RDialog) {
            if (((RDialog) (getRootPane().getParent())).getDialogType() == RDialog.DialogType.READONLY) {
                setEditable(false);
            } else {
                setEditable(customizeEditable);
            }
        }
    }

    /**
     * @return the customizeEditable
     */
    public boolean isCustomizeEditable() {
        return customizeEditable;
    }

    /**
     * @param customizeEditable the customizeEditable to set
     */
    public void setCustomizeEditable(boolean customizeEditable) {
        this.customizeEditable = customizeEditable;
    }

    /**
     * @return the customizeAlwaysEditable
     */
    public boolean isCustomizeAlwaysEditable() {
        return customizeAlwaysEditable;
    }

    /**
     * @param customizeAlwaysEditable the customizeAlwaysEditable to set
     */
    public void setCustomizeAlwaysEditable(boolean customizeAlwaysEditable) {
        this.customizeAlwaysEditable = customizeAlwaysEditable;
    }
}
